package douyin

import (
	"douhu_backend/internal/controller/common"
	"fmt"

	logger "douhu_backend/internal/log"

	"github.com/kataras/iris/v12"
)

// @license.name  Apache 2.0
// @license.url   http://www.apache.org/licenses/LICENSE-2.0.html
// @BasePath  /api/
type Base struct {
}

// 返回失败
func (b *Base) Error(ctx iris.Context, code int, message string, err error) {
	logMsg := message

	if err != nil {
		logMsg += fmt.Sprintf(" error:%s", err.Error())
	}

	logger.L.Error(logMsg)
	err = ctx.JSON(common.RspProto{
		Code:      code,
		Message:   message,
		Data:      nil,
		Error:     logMsg,
		RequestID: ctx.Request().Header.Get("X-Request-Id"),
	})
	if err != nil {
		logger.L.Errorf("response json err: %v", err)
	}
}

// 返回失败
func (b *Base) ErrorWithLog(ctx iris.Context, code int, message string, err error, logStr string) {
	logMsg := message

	if err != nil {
		logMsg += fmt.Sprintf(" error:%s", err.Error())
	}

	logger.L.Error(logMsg)
	err = ctx.JSON(common.RspProto{
		Code:      code,
		Message:   message,
		Data:      logStr,
		Error:     logMsg,
		RequestID: ctx.Request().Header.Get("X-Request-Id"),
	})
	if err != nil {
		logger.L.Errorf("response json err: %v", err)
	}
}

// 返回成功
func (b *Base) Success(ctx iris.Context, data interface{}) {
	if data == nil {
		data = []int{}
	}
	err := ctx.JSON(common.RspProto{
		Code:      OK.Code,
		Message:   OK.Message,
		Data:      data,
		Error:     "",
		RequestID: ctx.Request().Header.Get("X-Request-Id"),
	})
	if err != nil {
		logger.L.Errorf("response json err: %v", err)
	}
}

func (b *Base) Check(s string) bool {
	// TODO: Implement sensitive word checking
	return true
}
